#include "DeadlineTask.h"

// @author A0096955J
// this constructor creates a default DeadlineTask initalised with
// default values
DeadlineTask::DeadlineTask(){
	FloatingTask();

	setTimeAndDate(time(&timeAndDateTime_t));
	// initalising default values of time and date
	timeAndDateTmFormat.tm_hour = CommonConstant::DEFAULT_TIME_HOURS;
	timeAndDateTmFormat.tm_min = CommonConstant::DEFAULT_TIME_MINUTES;
	timeAndDateTmFormat.tm_sec = CommonConstant::DEFAULT_TIME_SECONDS;

	setTimeAndDate(timeAndDateTmFormat);

	taskType = CommonConstant::TASK_TYPE_DEADLINE;
}

// this constructor creates a DeadlineTask with values from a FloatingTask
DeadlineTask::DeadlineTask(FloatingTask* curr){
	FloatingTask();

	setTitle(curr->getTitle());
	setDescription(curr->getDescription());
	setPerson(curr->getPerson());
	setPriority(curr->getPriority());
	setLocation(curr->getLocation());
	setIndex(curr->getIndex());
	setFinishedStatus(curr->getFinishedStatus());
	setTimeAndDate(time(&timeAndDateTime_t));
	// initalising default values of time and date
	timeAndDateTmFormat.tm_hour = CommonConstant::DEFAULT_TIME_HOURS;
	timeAndDateTmFormat.tm_min = CommonConstant::DEFAULT_TIME_MINUTES;
	timeAndDateTmFormat.tm_sec = CommonConstant::DEFAULT_TIME_SECONDS;

	setTimeAndDate(timeAndDateTmFormat);
	taskType = CommonConstant::TASK_TYPE_DEADLINE;
}

// this constructor creates a DeadlineTask with the parameters passed into
// the constructor
DeadlineTask::DeadlineTask(string newTitle,
						   string newDescription, 
						   string newLocation, 
						   string newPerson,
						   int newPriority, 
						   int newFinishedStatus, 
						   int newIndex, 
						   time_t newTimeAndDate){
	setTitle(newTitle);
	setDescription(newDescription);
	setLocation(newLocation);
	setPerson(newPerson);
	setPriority(newPriority);
	setFinishedStatus(newFinishedStatus);
	setIndex(newIndex);
	setTimeAndDate(newTimeAndDate);
	taskType = CommonConstant::TASK_TYPE_DEADLINE;
}

DeadlineTask* DeadlineTask::clone() const{
	return new DeadlineTask (*this);
}
// returns time in string format HHMM where HH refers to hours and MM refers to minutes
// e.g 2200, 2359
string DeadlineTask::getTime(){
	strftime(CommonConstant::buffer,
			 CommonConstant::MAX_SIZE_TIME_STRING_FORMAT,
			 CommonConstant::TIME_24H_STRING_FORMAT.c_str(),
			 &timeAndDateTmFormat);

	return CommonConstant::buffer;
}

// returns date in string format DD/MM/YYYY where DD refers to day, MM refers to month
// and YYYY refers to year of the due date e.g 11/11/2013
string DeadlineTask::getDate(){
	strftime(CommonConstant::buffer,
			 CommonConstant::MAX_SIZE_DATE_STRING_FORMAT,
			 CommonConstant::DATE_STRING_FORMAT.c_str(),
			 &timeAndDateTmFormat);

	return CommonConstant::buffer;
}

// returns date in string format day, DD month year e.g Monday, 11 November 2013
string DeadlineTask::getFullDateInfo(){
	strftime(CommonConstant::day,
			 CommonConstant::MAX_SIZE_DAY_STRING_FORMAT,
			 CommonConstant::DAY_STRING_FORMAT.c_str(),
			 &timeAndDateTmFormat);
	strftime(CommonConstant::month,
			 CommonConstant::MAX_SIZE_MONTH_STRING_FORMAT,
			 CommonConstant::MONTH_STRING_FORMAT.c_str(),
			 &timeAndDateTmFormat);
	strftime(CommonConstant::year,
			 CommonConstant::MAX_SIZE_YEAR_STRING_FORMAT,
			 CommonConstant::YEAR_STRING_FORMAT.c_str(),
			 &timeAndDateTmFormat);

	sprintf_s(CommonConstant::buffer,
			  CommonConstant::FULL_DATE_INFO.c_str(),
			  CommonConstant::day,
			  to_string(timeAndDateTmFormat.tm_mday).c_str(),
			  CommonConstant::month,
			  CommonConstant::year);

	return CommonConstant::buffer;
}

time_t DeadlineTask::getTimeAndDate(){
	return timeAndDateTime_t;
}

struct tm DeadlineTask::getTimeAndDateTmFormat(){
	return timeAndDateTmFormat;
}

void DeadlineTask::setTimeAndDate(time_t newTimeAndDate){
	timeAndDateTime_t = newTimeAndDate;
	localtime_s(&timeAndDateTmFormat,&timeAndDateTime_t);
}

void DeadlineTask::setTimeAndDate(struct tm newTimeAndDate){
	timeAndDateTmFormat = newTimeAndDate;
	timeAndDateTime_t = mktime(&timeAndDateTmFormat);
}

// returns a string showing all task attributes in a informative way
string DeadlineTask::taskInfomation(){
	sprintf_s(CommonConstant::buffer,
			  CommonConstant::DEADLINE_TASK_STRING_FORMAT.c_str(),
			  FloatingTask::taskInfomation().c_str(),
			  getTime().c_str(),getDate().c_str(),
			  CommonConstant::PATTERN.c_str());

	return CommonConstant::buffer;
}

// returns a string showing only task title and time attributes in a informative way
string DeadlineTask::basicTaskInfomation(){
	sprintf_s(CommonConstant::buffer,
			  CommonConstant::DEADLINE_TASK_STRING_FORMAT.c_str(),
			  FloatingTask::taskInfomation().c_str(),
			  getTime().c_str(),getDate().c_str(),
			  CommonConstant::PATTERN.c_str());

	return CommonConstant::buffer;
}

DeadlineTask::~DeadlineTask(){
}
